
using System;
using System.Linq;
using Csla;
using Csla.Data;
using HousingInfo.DataLinQ;

namespace HousingInfo.Library
{ 
	public partial class CategoryBOList : Csla.BusinessListBase<CategoryBOList, CategoryBO>
	{

		#region Authorization Rules

		protected static void AddObjectAuthorizationRules()
		{
			//TODO: Define object-level authorization rules of CategoryBOList
			//AuthorizationRules.AllowCreate(typeof(CategoryBOList), "CategoryBOListCreateGroup"));
			//AuthorizationRules.AllowEdit(typeof(CategoryBOList), "CategoryBOListEditGroup"));
			//AuthorizationRules.AllowDelete(typeof(CategoryBOList), "CategoryBOListDeleteGroup"));
		}
		#endregion //Authorization Rules

        #region Trang Admin - Danh Muc

        #region Ham Lay danh sach tung danh muc
        public static CategoryBOList GetCategoryBOList_ByType(bool isnhadat, bool isdiendan, bool isngonngu, bool istintuc, bool isactive)
        {
            return DataPortal.Fetch<CategoryBOList>(new FilterCriteriaGetCategory_ByType(isnhadat, isdiendan, isngonngu, istintuc, isactive));
        }
        #endregion //End ham lay tung loai danh muc

        #region khai bao tham so
        private class FilterCriteriaGetCategory_ByType
        {
            public bool nhadat;
            public bool diendan;
            public bool ngonngu;
            public bool tintuc;
            public bool isative;
            public FilterCriteriaGetCategory_ByType(bool nd, bool dd, bool nn, bool tt, bool active)
            {
                this.nhadat = nd;
                this.diendan = dd;
                this.ngonngu = nn;
                this.tintuc = tt;
                this.isative = active;
            }
        }
        #endregion//end khai bao tham so

        #region Truy cap du lieu
        private void DataPortal_Fetch(FilterCriteriaGetCategory_ByType criteria)
        {
            bool cancel = false;
            OnFetching(ref cancel);
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                if (criteria.isative == true)
                {
                    if (criteria.diendan == true)
                    {
                        this.AddRange(
                            from row in mgr.DataContext.Categories
                            where row.IsTopicForum == true && row.IsActive == true
                            select CategoryBO.GetCategoryBO(row.CategoryID)
                        );
                    }
                    else if (criteria.ngonngu == true)
                    {
                        this.AddRange(
                            from row in mgr.DataContext.Categories
                            where row.IsLanguage == true && row.IsActive == true
                            select CategoryBO.GetCategoryBO(row.CategoryID)
                        );
                    }
                    else if (criteria.nhadat == true)
                    {
                        this.AddRange(
                            from row in mgr.DataContext.Categories
                            where row.IsHousingCategory == true && row.IsActive == true
                            select CategoryBO.GetCategoryBO(row.CategoryID)
                        );
                    }
                    else
                    {
                        this.AddRange(
                            from row in mgr.DataContext.Categories
                            where row.IsNewsCategory == true && row.IsActive == true
                            select CategoryBO.GetCategoryBO(row.CategoryID)
                        );
                    }
                }
                else
                {
                    if (criteria.diendan == true)
                    {
                        this.AddRange(
                            from row in mgr.DataContext.Categories
                            where row.IsTopicForum == true
                            select CategoryBO.GetCategoryBO(row.CategoryID)
                        );
                    }
                    else if (criteria.ngonngu == true)
                    {
                        this.AddRange(
                            from row in mgr.DataContext.Categories
                            where row.IsLanguage == true
                            select CategoryBO.GetCategoryBO(row.CategoryID)
                        );
                    }
                    else if (criteria.nhadat == true)
                    {
                        this.AddRange(
                            from row in mgr.DataContext.Categories
                            where row.IsHousingCategory == true
                            select CategoryBO.GetCategoryBO(row.CategoryID)
                        );
                    }
                    else
                    {
                        this.AddRange(
                            from row in mgr.DataContext.Categories
                            where row.IsNewsCategory == true
                            select CategoryBO.GetCategoryBO(row.CategoryID)
                        );
                    }
                }

                RaiseListChangedEvents = true;
            }//using


            //OnFetched();
        }
        #endregion//end truy cap

        #endregion //End Trang Admin Danh muc

        #region Ham lay ngau nhien 1 so danh muc tin tuc
        #region Ham
        public static CategoryBOList GetCategoryBOList_Random_ByNumber(int number)
        {
            return DataPortal.Fetch<CategoryBOList>(new FilterCriteriaGetCategory_Random_ByNumber(number));
        }
        #endregion //End ham lay tung loai danh muc

        #region khai bao tham so
        private class FilterCriteriaGetCategory_Random_ByNumber
        {
            public int soluong;
            public FilterCriteriaGetCategory_Random_ByNumber(int sl)
            {
                this.soluong = sl;
            }
        }
        #endregion//end khai bao tham so

        #region Truy cap du lieu
        private void DataPortal_Fetch(FilterCriteriaGetCategory_Random_ByNumber criteria)
        {
            bool cancel = false;
            OnFetching(ref cancel);
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    (from row in mgr.DataContext.Categories
                     where row.IsNewsCategory == true && row.IsActive == true
                    orderby new Random().Next()
                    select CategoryBO.GetCategoryBO(row.CategoryID)).Take(criteria.soluong)
                );

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion//end truy cap

        #endregion //End lay random cac danh muc

        #region Trang Category Kiem tra trung nhau khi them moi
        public static bool CheckCateName_WhenAddNew(string namecategory)
        {
            int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.Categories.Count(n => n.CategoryName == namecategory);
            }
            if (count > 0) return false;
            else return true;
        }
        #endregion

        #region Trang User - Xem Them Tin Tuc
        #region Ham lay 1 id danh muc khac id danh muc da co
        public static CategoryBOList GetCategoryBO_ByOrtherCurrentID(int idcurrent)
        {
            return DataPortal.Fetch<CategoryBOList>(new FilterCriteria_CurrentID(idcurrent));
        }
        #endregion //End ham lay tung loai danh muc

        #region khai bao tham so
        private class FilterCriteria_CurrentID
        {
            public int idCurrent;
            public FilterCriteria_CurrentID(int cr)
            {
                this.idCurrent = cr;
            }
        }
        #endregion//end khai bao tham so

        #region Truy cap du lieu
        private void DataPortal_Fetch(FilterCriteria_CurrentID criteria)
        {
            bool cancel = false;
            OnFetching(ref cancel);
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                this.AddRange(
                    (from row in mgr.DataContext.Categories
                     where row.IsNewsCategory == true && row.IsActive == true && row.CategoryID != criteria.idCurrent
                     orderby new Random().Next()
                     select CategoryBO.GetCategoryBO(row.CategoryID)).Take(1)
                );

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion//end truy cap

        #endregion //End lay random cac danh muc
    }
}

